{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9673f648-262a-4700-80d5-33627418171d",
   "metadata": {},
   "source": [
    "# 字符串的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea339b9c-71f2-42ba-b8f5-c87d49c7b44a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我在学Python语言'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'我在学Python语言'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "88083125-f679-4ebe-9c80-cd436ef24adc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我在学Python语言'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"我在学Python语言\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "395b5b3c-f85c-4fb5-ba80-b6e1bf5022b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我在学Python语言'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''我在学Python语言'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e5de3ca1-7410-44d7-83b0-9a8669df04b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我使用单双引号\\'a\\'和\"b\" '"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"我使用单双引号'a'和\"b\" \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a96b378b-d480-4a09-874d-66d3d31fca97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"老师，'七月流火'是什么意思？\""
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"老师，'七月流火'是什么意思？\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "38ef3fc4-73b2-4b2e-a751-8605e218b130",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '“' (U+201C) (2235882511.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[13], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    “abc”\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character '“' (U+201C)\n"
     ]
    }
   ],
   "source": [
    "“abc”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db84525f-b0c0-4115-a47d-10814b8668be",
   "metadata": {},
   "outputs": [],
   "source": [
    "'\\u'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6c5f3e3c-614f-4ef5-913e-b8767a1ab818",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'© ❤ ☺ ☺'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'\\u00A9 \\u2764 \\u263A \\U0000263A'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c5019f99-ecd0-40b9-a7b1-99511a608fda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'富强、民主、文明、和谐，\\n自由、平等、公正、法治，\\n爱国、敬业、诚信、友善。'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"富强、民主、文明、和谐，\\n自由、平等、公正、法治，\\n爱国、敬业、诚信、友善。\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ade514f5-f74a-4916-9131-324ce4398b1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n富强、民主、文明、和谐，\\n自由、平等、公正、法治，\\n爱国、敬业、诚信、友善。\\n'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "富强、民主、文明、和谐，\n",
    "自由、平等、公正、法治，\n",
    "爱国、敬业、诚信、友善。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0e664692-b8e3-4924-9634-f2ae058375f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'富强、民主、文明、和谐，\\n自由、平等、公正、法治，\\n爱国、敬业、诚信、友善。'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\\\n",
    "富强、民主、文明、和谐，\n",
    "自由、平等、公正、法治，\n",
    "爱国、敬业、诚信、友善。\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96ef2145-293f-4122-8a10-75ad8e29c6c1",
   "metadata": {},
   "source": [
    "# 字符串拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fa41aa90-c742-4e9f-bed9-10c85cadc21d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我在学习Python'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"我\" + \"在\" + \"学习Python\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "88935331-9abe-4a7a-bf16-df89142f5177",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABCXYZ'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"ABC\" \"XYZ\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38358613-5420-44ff-969b-0ca3a73f65bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "156b1f03-7a97-4012-b961-3b73baf6724a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "077e71d9-bdd3-4632-8794-78e55b9cf757",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "da53fa59-4cd8-41d9-9a34-1c362cae969d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'E'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "579e1c85-0ad9-4b31-a668-900b212246f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'BCD'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "88ca70b4-c7f0-41bc-8332-a48326c9a178",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'H'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "8f743870-fe1a-4a2b-8bfa-3486832f030e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'BCD'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[-7:-4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "12c21319-3652-41d3-ba5c-0ccd8ea37dae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HGFEDCBA'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "b3bc4ec4-edbe-4b8c-a1c5-3aea227e3890",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HGFEDCBA'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[-1:-9:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "b5f56082-87c2-4033-ab15-f573a998b466",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HGFEDCB'"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[8:0:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "41311f27-c852-42c6-bc15-39448fa3b0aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HGFEDCBA'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'ABCDEFGH'\n",
    "s[8:-9:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ce472def-db58-4b27-a671-17f68b57f8d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'' in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ba11468-a3b2-4b45-af26-1a1471efdc87",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6d246e8c-9e89-4666-9c86-f1c0f8b443cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2faa487a-00c4-4f60-bbae-0221babd83f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "985a3aea-7041-4555-87f9-26ad4817d752",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'H'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1e250b27-0fc1-4dda-afdd-498362d39f02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HGF'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[7:4:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "93a37d07-4e46-4565-846f-286b1545b3c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HGFEDCBA'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-1:-9:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "34c6f031-92c0-4d8e-b2bb-65fcfbde750e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'FEDCB'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[5:0:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2ca4ac69-d45d-4f98-af42-cf0508d1e3b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABCDEF'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-8:-2:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f822948d-2056-40b3-97e9-5ba5c06928f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'H'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-1:-2:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed61ba89-be9b-47c3-83c0-7fdb3e77338f",
   "metadata": {},
   "source": [
    "# 常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ec798ec2-822e-4a62-9a70-93c4f42c5522",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '我用Python'\n",
    "\n",
    "len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a88eca2-4df6-40fb-b150-91409c9f1118",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "65d60d44-b509-48a3-8a14-0c44a9c55475",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Python' in '我用Python'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "d785732b-b081-4548-8600-ea552f8c5e62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Rust' in '我用Python'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69512f8e-561e-4a97-ae36-99ccb5e7426c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "25837386-b101-42f1-a98d-c258873e1f69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '我用Python'\n",
    "s.find('Python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "61d5c7c7-203a-40df-ac64-7d80578fde70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '我用Python'\n",
    "s.find('Rust')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "1dc90b12-2fdd-4314-a81f-5880af39f13a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我用Python'"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '  我用Python  '\n",
    "s.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6315a312-98aa-4249-b405-597798dfcc28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我喜欢Python'"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '我用Python'\n",
    "s.replace('用', '喜欢')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "8b6871fe-c20e-4e88-bb70-b207a87849ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我用Python'"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '  我用Python  '\n",
    "s.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "1b7a3203-0933-435c-8a11-ef7821639db6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我用Python  '"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '  我用Python  '\n",
    "s.lstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "ae6fc1b2-0b39-4b33-bd1d-f3cdde2ffec1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'  我用Python'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '  我用Python  '\n",
    "s.rstrip()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
